<?php

namespace Xin\Hyperf\Fortify\Facades;

use Hyperf\Context\ApplicationContext;
use Hyperf\Redis\RedisFactory;
use Hyperf\Redis\RedisProxy;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Xin\Support\Facade;

/**
 * @mixin \Hyperf\Redis\Redis
 */
class Redis extends Facade
{
    /**
     * @param string|null $connection
     * @return RedisProxy
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function getConnection(string $connection = null): RedisProxy
    {
        return self::getFacadeFactoryAccessor()->get($connection ?: 'default');
    }

    /**
     * @return RedisProxy
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function getFacadeAccessor(): RedisProxy
    {
        return self::getConnection();
    }

    /**
     * @return RedisFactory
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function getFacadeFactoryAccessor(): RedisFactory
    {
        return ApplicationContext::getContainer()->get(RedisFactory::class);
    }
}
